import 'package:flutter/material.dart';
import 'package:flutter_app/customwidget/base_widget/ButtonWidget.dart';
import 'package:flutter_app/util/DataUtil.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';

import 'CheckBoxWidget.dart';
import 'FieldFormWidget.dart';
import 'IconWidget.dart';
import 'ProgressWidget.dart';
import 'TextWidget.dart';

/**
 * listview列表
 */
class BaseListWidget extends StatelessWidget {
  BaseListWidget({
    Key ?key,
    @required this.text, // 接收一个text参数
  }) : super(key: key);
  final String ?text;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(text??""),
      ),
      body: BaseListPage(),
    );
  }
}

class BaseListPage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<BaseListPage> {
  var list = DataUtil.getBaseTitle();

  @override
  Widget build(BuildContext context) {
    return MasonryGridView.count(crossAxisCount: 4,
        mainAxisSpacing:4,crossAxisSpacing:4,itemBuilder: _buildItem);
    // return StaggeredGridView.countBuilder(
    //   crossAxisCount: 4,
    //   itemCount: list.length,
    //   itemBuilder: _buildItem,
    //   staggeredTileBuilder: (int index) =>
    //   new StaggeredTile.count(2, index.isEven ? 1.5 : 1),
    //   // mainAxisSpacing: 2.0,
    //   // crossAxisSpacing: 2.0,
    // );

    ListView.builder(
      itemCount: list.length,
      itemBuilder: _buildItem,
      itemExtent: 60,
    );
  }
}

Widget _buildItem(BuildContext context, int index) {
  var list = DataUtil.getBaseTitle();
  return Container(
    padding: EdgeInsets.only(top: 10, left: 10, right: 10),
    alignment: Alignment.center,
    child: GestureDetector(
      child: DecoratedBox(
        decoration: BoxDecoration(
            gradient: LinearGradient(colors: [Colors.deepOrange, Colors.deepPurple]),
            //背景渐变
            borderRadius: BorderRadius.circular(3.0),
            boxShadow: [
              BoxShadow(
                  color: Colors.grey, offset: Offset(2.0, 2.0), blurRadius: 4.0)
            ]),
        child: Center(
          child: Text(
            list[index],
            style: TextStyle(fontSize:15, color: Colors.white),
          ),
        ),
      ),
      onTap: () {
        toArount(index, context, list[index]);
      },
    ),
  );
}

void toArount(int index, BuildContext context, String title) {
  switch (index) {
    case 0:
      goTo(context, TextWidget(text: title));
      break;
    case 1:
      goTo(context, ButtonWidget(text: title));
      break;
    case 2:
      goTo(context, IconWidget(text: title));
      break;
    case 3:
      goTo(context, CheckBoxWidget(text: title));
      break;
    case 4:
      goTo(context, FieldFormWidget(text: title));
      break;
    case 5:
      goTo(context, ProgressWidget(text: title));
      break;
  }
}

void goTo(BuildContext context, Widget widget) {
  Navigator.push(context, MaterialPageRoute(builder: (context) {
    return widget;
  }));
}
